package snsdbook.servlets.likes;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.SQLUtil;
import util.Util;
import examples.SQLExample;

/**
 * Servlet implementation class LikeQueryServlet
 */
public class LikeQueryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LikeQueryServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {	  
	  String query = null;
	  if (request.getParameter("type").equals("likes")) {
	    query = "SELECT COUNT(id_likes) AS cnt FROM likes " +
              "WHERE id_berita='" +
              request.getParameter("id") +
              "'";
	  } else if (request.getParameter("type").equals("dislike")) {
	    query = "SELECT COUNT(id_dislike) AS cnt FROM dislike " +
              "WHERE id_berita='" +
              request.getParameter("id") +
              "'";
	  } else {
	  SQLExample.main(null);
	    Util.PrintAndClose(response, "UNRECOGNIZED PARAMETERS");
	    return;
	  }
	  
	  String[] answers = SQLUtil.Query(query);
	  assert answers.length == 1;
	  
	  Util.PrintAndClose(response, answers[0]);
	}

}
